package com.sp2p.service;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.shove.base.BaseService;
import com.shove.data.DataSet;
import com.shove.data.dao.MySQL;
import com.shove.util.BeanMapUtils;
import com.shove.vo.PageBean;
import com.sp2p.dao.GofullAwardDao;
import com.sp2p.dao.admin.GroupUserDao;

public class GoFullAwardService extends BaseService {

	public static Log log = LogFactory.getLog(GoFullAwardService.class);

	private GofullAwardDao gofullAwardDao;
	private GroupUserDao groupUserDao;
	public  GroupUserDao getGroupUserDao(){
		return groupUserDao;
	}
	public void setGroupUserDao(GroupUserDao groupUserDao) {
		this.groupUserDao = groupUserDao;
	}
	public GofullAwardDao getGofullAwardDao() {
		return gofullAwardDao;
	}
	/**
	 * 添加共富奖励
	 * @param userId
	 * @param investor
	 * @param awardMoney
	 * @param status
	 * @return
	 * @throws Exception
	 */
	public long addGofullAward(long userId,long investor,BigDecimal awardMoney,int status,BigDecimal amountDouble,long borrowId)
			throws Exception {
		Connection conn = MySQL.getConnection();
		long result=0;
		try {
			
			result = gofullAwardDao.addGofullAward(conn, userId, investor, awardMoney, status,amountDouble,borrowId);
			if(result>0){
				conn.commit();
			}
			
		} catch (Exception e) {
			log.error(e);
			e.printStackTrace();
			conn.rollback();
			throw e;
		} finally {
			conn.close();
		}

		return result;
	}
	/**
	 * 查询共富奖励
	 * @param userId
	 * @param investor
	 * @param status
	 * @param startDate
	 * @param endDate
	 * @param pageBean
	 * @throws Exception
	 */
	public void queryGofullAward(Long userId, Long investor,
			Integer status, String startDate, String endDate, PageBean<Map<String, Object>> pageBean)
			throws Exception {
		StringBuffer condition = new StringBuffer();
		if (userId != null && userId > 0) {
			condition.append(" AND userId = " + userId);
		}
		if (investor != null && investor > 0) {
			condition.append(" AND investor = " + investor);
		}
		if (status != null && status > 0) {
			condition.append(" AND status = " + status);
		}
		if (StringUtils.isNotBlank(startDate)) {
			condition.append(" AND investTime >= '"
					+ StringEscapeUtils.escapeSql(startDate) + "'");
		}
		if (StringUtils.isNotBlank(endDate)) {
			condition.append(" AND investTime <= '"
					+ StringEscapeUtils.escapeSql(endDate) + "'");
		}
		condition.append(" order by investTime DESC ");

		Connection conn = MySQL.getConnection();
		try {
			dataPage(conn, pageBean, " v_gofull_award a", " a.*,f_formatting_username(a.investname) as encInvestname ", "", condition
					.toString());
		} catch (Exception e) {
			log.error(e);
			e.printStackTrace();
			
			throw e;
		} finally {
			conn.close();
		}
	}
	
	/**
	 * 共富奖励统计（前台）
	 * @param userId
	 * @param investor
	 * @param status
	 * @param startDate
	 * @param endDate
	 * @param pageBean
	 * @throws Exception
	 */
	public void gofullAwardStatisc(Long userId, Long investor,
			Integer status, String startDate, String endDate, PageBean<Map<String, Object>> pageBean)
			throws Exception {
		StringBuffer condition = new StringBuffer();
		if (userId != null && userId > 0) {
			condition.append(" AND userId = " + userId);
		}
		if (investor != null && investor > 0) {
			condition.append(" AND investor = " + investor);
		}
		if (status != null && status > 0) {
			condition.append(" AND status = " + status);
		}
		if (StringUtils.isNotBlank(startDate)) {
			condition.append(" AND investTime >= '"
					+ StringEscapeUtils.escapeSql(startDate) + "'");
		}
		if (StringUtils.isNotBlank(endDate)) {
			condition.append(" AND investTime <= '"
					+ StringEscapeUtils.escapeSql(endDate) + "'");
		}

		Connection conn = MySQL.getConnection();
		try {
			dataPage(conn, pageBean, " v_gofull_award ", " * ", "", condition
					.toString());
		} catch (Exception e) {
			log.error(e);
			e.printStackTrace();
			
			throw e;
		} finally {
			conn.close();
		}
	}
	
	/**
	 * 获得月共富奖励总和
	 * @param userId
	 * @param flag 月份标识 0 本月  1上月  -1 下月
	 * @return
	 * @throws Exception
	 */
	public Map<String, String> queryOneMonthAward(Long userId,int flag) throws Exception {
		Connection conn = MySQL.getConnection();
		try{
			StringBuffer condition = new StringBuffer();
			condition.append(" select ifnull(round(sum(g.awardMoney),2),0.00) as lastMonth from t_gofull_award g  WHERE " +
					"PERIOD_DIFF( date_format( now() , '%Y%m') , date_format( g.operateTime, '%Y%m' ) ) ="+flag);
			condition.append(" and userId="+userId);
			String countSql =condition.toString();
			DataSet dataSet = MySQL.executeQuery(conn, countSql);
			condition=null;
			return BeanMapUtils.dataSetToMap(dataSet);
		} catch (Exception e) {
			log.error(e);
			e.printStackTrace();
			throw e;
		} finally {
			conn.close();
		}
	}
	
	public Map<String, String> queryAwardNotSettled(Long userId) throws Exception {
		Connection conn = MySQL.getConnection();
		try{
			StringBuffer condition = new StringBuffer();
			condition.append(" select ifnull(round(sum(g.awardMoney),2),0.00) as awardMoney from t_gofull_award g where g.status=1 ");
			condition.append(" and userId = "+userId);
			String countSql =condition.toString();
			DataSet dataSet = MySQL.executeQuery(conn, countSql);
			condition=null;
			return BeanMapUtils.dataSetToMap(dataSet);
		} catch (Exception e) {
			log.error(e);
			e.printStackTrace();
			throw e;
		} finally {
			conn.close();
		}
	}
	
	/**
	 * 获得累计结算总收入
	 * @param userId
	 * @return
	 * @throws Exception
	 */
	public Map<String, String> queryAward(Long userId) throws Exception {
		Connection conn = MySQL.getConnection();
		try{
			StringBuffer condition = new StringBuffer();
			condition.append(" select ifnull(round(sum(g.awardMoney),2),0.00) as awardMoney from t_gofull_award g where g.status=2");
			condition.append(" and userId="+userId);
			String countSql =condition.toString();
			DataSet dataSet = MySQL.executeQuery(conn, countSql);
			condition=null;
			return BeanMapUtils.dataSetToMap(dataSet);
		} catch (Exception e) {
			log.error(e);
			e.printStackTrace();
			throw e;
		} finally {
			conn.close();
		}
	}

	public void setGofullAwardDao(GofullAwardDao gofullAwardDao) {
		this.gofullAwardDao = gofullAwardDao;
	}

	/**
	 * 添加共富奖励
	 * @param userId
	 * @param investor
	 * @param awardMoney
	 * @param status
	 * @return
	 * @throws Exception
	 */
	public long updateGofullAward(String id)
			throws Exception {
		Connection conn = MySQL.getConnection();
		long result=0;
		try {
			
			result = gofullAwardDao.updateGofullAward(conn, id);
			if(result>0){
				conn.commit();
			}
			
		} catch (Exception e) {
			log.error(e);
			e.printStackTrace();
			conn.rollback();
			throw e;
		} finally {
			conn.close();
		}

		return result;
	}
	/**
	 * 统计共富奖励总额
	 * 
	 * @return
	 * @throws SQLException
	 */

	public Map<String, String> queryTotalGofullAward() throws Exception {
		Connection conn = MySQL.getConnection();

		Map<String, String> map = new HashMap<String, String>();
		try {
			map = gofullAwardDao.queryTotalGofullAward(conn);
		} catch (Exception e) {
			log.error(e);
			e.printStackTrace();
			throw e;
		} finally {
			conn.close();
		}
		return map;
	}
	/**
	 * 查询内部员工组
	 * 
	 * @return
	 * @throws SQLException
	 */

			
	public Map<String, String> queryGroupUser(int groupID,int userID) throws Exception {
		Connection conn = MySQL.getConnection();

		Map<String, String> map = new HashMap<String, String>();
		try {
			map = groupUserDao.queryGroupUser(conn, groupID, userID);
		} catch (Exception e) {
			log.error(e);
			e.printStackTrace();
			throw e;
		} finally {
			conn.close();
		}
		return map;
	}
}
